{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is intended to demonstrate how registration methods of ITKTubeTK operate.\n",
    "\n",
    "There are many other (more effective) registration methods available in other pages (e.g., NiftiReg, ANTS).  However, the method in ITKTubeTK is often sufficient and is easy to control.  So, if it works for your problem..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "\n",
    "from itkwidgets import view\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "im1 = itk.imread(\"Data/MRI-Cases/mra.mha\", itk.F)\n",
    "im2 = itk.imread(\"Data/MRI-Cases/mri_t1_sag.mha\", itk.F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "ImageType = itk.Image[itk.F,3]\n",
    "\n",
    "reg2To1 = ttk.RegisterImages[ImageType].New(FixedImage=im1, MovingImage=im2)\n",
    "reg2To1.SetReportProgress(True)\n",
    "reg2To1.SetExpectedOffsetMagnitude(5)\n",
    "reg2To1.SetExpectedRotationMagnitude(0.005)\n",
    "reg2To1.SetRegistration(\"PIPELINE_AFFINE\")\n",
    "reg2To1.SetMetric(\"MATTES_MI_METRIC\")\n",
    "reg2To1.Update()\n",
    "im2Reg = reg2To1.ResampleImage()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "im2RegMath = ttk.ImageMath[ImageType,ImageType].New(Input=im2Reg)\n",
    "im2RegMath.IntensityCorrection(100,4,im1)\n",
    "im2RegInt = im2RegMath.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1e67d7644e304b389c8cd7fec1570e0a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cb12 = itk.CheckerBoardImageFilter[ImageType].New(Input1=im1, Input2=im2RegInt)\n",
    "cb12.Update()\n",
    "im12 = ImageType.New()\n",
    "im12 = cb12.GetOutput()\n",
    "view(im12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
